<?php

class TestDataBean_PHP5 extends UnitTestCase {

    function testMemberGetMagic() {
        $b=new DatabeanChild_php5();
        $this->assertEqual($b->generated,date('Y-m-d'),'should be generated when getted');
        $this->assertEqual($b->date,date('Y-m-d'),'should have auto value');
        $this->assertEqual($b->choice,'default','should have default value');

        $this->assertEqual($b->custom_getter,13,'custom getters should override output filters, %s');
    }

    function testInputFilterMagic() {
        $b=new DatabeanChild_php5();
        $str='to:toto@test.net hello world';
        $b->str=$str;
        $this->assertEqual($b->str,strtoupper($str));
        $b->plus_one=12;
        $this->assertEqual($b->plus_one,13);
    }

    function testOutputFilterMagic() {
        $b=new DatabeanChild_php5();
        $b->hello='darling';
        if (!$this->assertEqual($b->hello,'hello darling')) {
            $this->dump($b);
        }
    }

    function testSetMagic() {
        $b=new DatabeanChild_php5();
        $b->int=12;
        $b->int='36';
        $this->assertEqual($b->int,12);

        $b->choice='choucroute';
        $b->choice=null;
        $b->choice='';
        $this->assertEqual($b->choice,'default');

        // custom setter
        $b->custom_setter='some value';
        $this->assertEqual($b->custom_setter,'some value:10');
    }

    function testListMembers() {
        $b=new DatabeanChild_php5();
        $list=$b->listMembers();
        $this->assertIsA($list,'array');
        $this->assertEqual($list,array(
            'int','bool','str','choice','date'
            ,'custom_getter','custom_setter','non_gettable','non_settable'
            ,'plus_one','hello'));
        // $this->dump($list);
    }

    function testListRequiredMembers() {
        $b=new DatabeanChild_php5();
        $list=$b->listRequiredMembers();
        $this->assertIsA($list,'array');
        $this->assertEqual($list,array(
            'int','bool','str','choice','date'));
    }

    function testCall() {
        $b=new DataBean();
        $this->assertTrue($b->setVar('value'));
        $this->assertEqual($b->getVar(),'value');
    }
}

class DatabeanChild_php5 extends DataBean {

    function __construct($data=array()) {
        $this->membersLock(true);
        parent::__construct($data);
    }

    /**
     * @var int a simple integer
     */
    protected $int;
    function _int_check($x) {
        return is_int($x);
    }
    function _int_required() { return true; }

    /**
     * @var bool
     */
    protected $bool=false;
    function _bool_inputfilter($x) {
        return $x?true:false;
    }
    function _bool_required() { return true; }

    /**
     * @var string will be converted upper case on input
     */
    protected $str;
    function _str_check($x) {
        return is_string($x);
    }
    function _str_inputFilter($x) {
        return strtoupper($x);
    }
    function _str_required() { return true; }

    /**
     * @var string should belong to an items list
     */
    protected $choice='default';
    function _choice_check($x) {
        return in_array($x,array('pizza','hamburger'));
    }
    function _choice_required() { return true; }

    /**
     * @var string a date, MySQL format, settign auto to current
     */
    protected $date;
    function _date_autoValue() {
        return date('Y-m-d');
    }
    function _date_check($x) {
        return ereg('[0-9]{4}-[0-9]{2}-[0-9]{2}',$x);
    }
    function _date_required() { return true; }

    /**
     * @var string an undeclared, generated member
     */
    function _generated_get() {
        return date('Y-m-d');
    }

    /**
     * @var string a member with a custom getter, overriding output filter
     */
    protected $custom_getter='custom getter';
    function _custom_getter_get() {
        return strlen($this->custom_getter);
    }
    function _custom_getter_outputFilter($x) {
        return strtoupper($x);
    }

    /**
     * @var string a member with its custom setter, overriding checks and input filter
     */
    protected $custom_setter='custom setter';
    function _custom_setter_set($x) {
        // echo '<p>used custom setter </p>';
        $this->custom_setter=$x.':'.strlen($x);
        return true;
    }
    function _custom_setter_inputFilter($x) {
        return strtoupper($x);
    }
    function _custom_setter_check($x) {
        // only numeric strings
        return ereg('^[0-9]+$',$x);
    }

    /**
     * @var string a "non-gettable" member
     */
    protected $non_gettable='non gettable';
    function _non_gettable_gettable() {
        return false;
    }

    /**
     * @var string an "non-settable" member
     */
    protected $non_settable='non settable';
    function _non_settable_settable() {
        return false;
    }

    /**
     * @var int will be set as value+1
     */
    protected $plus_one;
    function _plus_one_inputFilter($x) {
        return intval($x)+1;
    }
    function _plus_one_check($x) {
        return is_int($x);
    }

    /**
     * @var string will be outputted as 'hello '.value
     */
    protected $hello;
    function _hello_outputFilter($x) {
        return 'hello '.$x;
    }
}
